﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void SaveWTSAER(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            try
            {
                DataSet setResult = new DataSet();
                DataTable tbLocal = null;
                DataRow dwLocal = null;
                bool flgExist = false;
                setResult = QueryWTSAER(dwData);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];
                    if (tbLocal.Rows.Count > 0)
                    {
                        flgExist = true;
                        dwLocal = tbLocal.Rows[0];
                    }
                    else
                    {
                        dwLocal = tbLocal.NewRow();
                    }
                }

                MapWTSAER(ref dwLocal, dwData);

                if (!flgExist)
                {
                    InsertWTSAER(dwLocal);
                }
                UpdateWTSAER(dwLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex.Message);
            }
        }
        public DataSet QueryWTSTR(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1 = OracleDB.CreateCommand();
            // APZMANDANT  APZART APZVAR APZANRKRS APZALFDNR APZSTUFE
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM WTSTR WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM WTSTR WHERE  "
                    + "  WTIMANDANT=:WTIMANDANT AND WTIART=:WTIART AND WTIVAR=:WTIVAR";
                    or1.Parameters.Add("WTIMANDANT", OracleType.NVarChar).Value = dwData["WTIMANDANT"].ToString();
                    or1.Parameters.Add("WTIART", OracleType.NVarChar).Value = dwData["WTIART"].ToString();
                    or1.Parameters.Add("WTIVAR", OracleType.NVarChar).Value = dwData["WTIVAR"].ToString();
                    break;
                case 2:
                default:
                    or1.CommandText = "SELECT * FROM WTSTR WHERE  "
                + "  WTIMANDANT=:WTIMANDANT AND WTIART=:WTIART AND WTIVAR=:WTIVAR";
                    or1.Parameters.Add("WTIMANDANT", OracleType.NVarChar).Value = dwData["WTIMANDANT"].ToString();
                    or1.Parameters.Add("WTIART", OracleType.NVarChar).Value = dwData["WTIART"].ToString();
                    or1.Parameters.Add("WTIVAR", OracleType.NVarChar).Value = dwData["WTIVAR"].ToString();
                    break;
            }

            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "WTSTR";
            }
            setReturn.Merge(Result);
            return setReturn;
        }
    }
}
